<?php

namespace App\System\Base\Services;

use App\Exceptions\Customize\ParamsException;
use App\System\Base\Helpers\Traits\SinglePattern;

class SignService extends Service
{
    use SinglePattern;

    /**
     * 检查签名
     */
    public static function checkSign()
    {
        $params = request()->all();
        $sign = request()->header('m');

        if (!$sign) {
            throw new ParamsException([
                'error_code' => 10003,
                'message'    => '缺少签名信息'
            ]);
        }

        $json_params = json_encode($params);

        if ($sign != md5($json_params . config('global.sign_key'))) {
            throw new ParamsException([
                'error_code' => 10004,
                'message'    => '签名错误'
            ]);
        }

        return true;
    }
}